package io.lckgroup.modules.lck.service.impl;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mchange.v2.beans.BeansUtils;
import io.lckgroup.common.utils.PageUtils;
import io.lckgroup.common.utils.Query;
import io.lckgroup.modules.lck.dao.CustBaseDao;
import io.lckgroup.modules.lck.entity.CustBaseEntity;
import io.lckgroup.modules.lck.entity.CustomerInfoEntity;
import io.lckgroup.modules.lck.entity.qo.CustBaseQo;
import io.lckgroup.modules.lck.entity.qo.updateCustBaseQo;
import io.lckgroup.modules.lck.service.CustBaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("CustBaseService")
public class CustBaseServiceImpl extends ServiceImpl<CustBaseDao, CustBaseEntity> implements CustBaseService {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private CustBaseDao custBaseDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CustBaseEntity> page = this.page(
                new Query<CustBaseEntity>().getPage(params),
                new QueryWrapper<CustBaseEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 功能描述: <br>
     * 〈〉 使用openid查询当前用户是否存在
     *
     * @Param: [customerInfoEntity]
     * @Return: 不存在 null   存在 返回实体
     * @Author: huangrui
     * @Date: 2020/2/11 15:05
     */
    @Override
    public List<CustBaseEntity> getCustomerByUserId(String userId) {
        QueryWrapper<CustBaseEntity> customWrapper = new QueryWrapper<>();
        customWrapper.eq("user_id", userId);
        List<CustBaseEntity> entities = baseMapper.selectList(customWrapper);
        return entities;
    }

    @Override
    public CustBaseEntity getChild(String id) {
        System.out.println(id);
        CustBaseEntity custBaseEntity = baseMapper.selectById(Long.valueOf(id));
        return custBaseEntity;
    }

    @Override
    public boolean updateChild(updateCustBaseQo qo) {
        CustBaseEntity  entity = CustBaseEntity.builder()
                .addr(qo.getAddr())
                .age(qo.getAge())
                .id(qo.getId())
                .name(qo.getName())
                .sex(qo.getSex())
                .goDate(qo.getGoDate())
                .phone(qo.getPhone())
                .build();
        logger.info("用户更新："+entity);
        baseMapper.updateById(entity);
        if(qo.getRole().equals("1")) {
            entity.setUserId(qo.getUserId());
            // 家长同步更新电话和地址
            int i = baseMapper.updateAddrPhoneByUserId(entity);
        }
        return true;
    }

    @Override
    public boolean insertChild(updateCustBaseQo qo) {
        CustBaseEntity  entity = CustBaseEntity.builder()
                .addr(qo.getAddr())
                .age(qo.getAge())
                .role(qo.getRole())
                .userId(qo.getUserId())
                .name(qo.getName())
                .sex(qo.getSex())
                .goDate(qo.getGoDate())
                .phone(qo.getPhone())
                .createDate(new Date())
                .updateDate(new Date())
                .build();
        logger.info("用户更新："+entity);
        baseMapper.insert(entity);
        return true;
    }

    @Override
    public boolean deleteChild(String id) {
        int i = baseMapper.deleteById(Integer.valueOf(id));
        return true;
    }

    /**
     * 功能描述: <br>
     * 〈〉保存当前custom信息，并且返回完整信息
     *
     * @Param: [customerInfoEntity]
     * @Return: 保存当前custom信息，并且返回完整信息,成功返回对象，失败返回null
     * @Author: huangrui
     * @Date: 2020/2/11 15:07
     */
    @Override
    public CustBaseEntity saveAndSelectCustomer(CustBaseEntity customer) {
        baseMapper.insert(customer);//插入用户数据
        QueryWrapper<CustBaseEntity> customWrapper = new QueryWrapper<>();
        customWrapper.setEntity(customer);
        CustBaseEntity CustomerInfoEntity = baseMapper.selectOne(customWrapper);//查询出该用户数据
        return CustomerInfoEntity;
    }

    @Override
    public boolean saveUserBase(CustomerInfoEntity customerInfoEntity,CustBaseQo customer) {
        boolean b = true;
        logger.info("用户补录入参："+customer);
        try {
            for (CustBaseEntity entity : customer.getCustBaseList()) {
                entity.setUserId(customerInfoEntity.getId());
                entity.setCreateDate(new Date());
                entity.setUpdateDate(new Date());
                int insert = baseMapper.insert(entity);
                if(insert>0){
                    b=true;
                }
            }
        } catch (Exception e){
            logger.info("用户补录入参异常{}",e.getMessage());
            b=true;
            return b;
        }
        return b;
    }


}